
import { Dialog } from 'vant';
// import Hybrid from '@/components/uzone-hybrid';
import spreadLoanPreview from '@/components/spreadLoanPreview';

export default {
  name: 'preview',
  components: {
    spreadLoanPreview
  },
  props: {
    data: {
      type: Object,
      default: {}
    },
    extensionamount: {
      default: 0
    },
    payintype: {
      default: ''
    },
    type: {
      default: '',
    },
    repayType: {
      default: '',
    },
    repayCode: {
      default: '',
    }
  },
  data () {
    return {
      isVisibleNumQuestion: false,
      isVisibleDetails: false,
      isVisibleQuestion: false,
      couponInfo: {}, // 优惠券信息
      tempCouponInfo: {}, // 优惠券信息
      orderId: '',
      timer: -1, // 计时器
      couponList: [], // 优惠券列表
      showCouponList: false, // 是否显示优惠券列表弹框
    };
  },
  computed: {
    // 还款金额
    repayAmount() {
      let amount = 0;
      const { data, couponInfo } = this;
      // 0未逾期未还款，1逾期未还款，2逾期已还款，3未逾期已还款
      if(data.isInstallment==1){
        let amount = 0;
        let nowDate = Date.parse(new Date());
        let maxNub = 0
        for(let i =0;i<data.repayPeriodScheduleDtos.length;i++){
            let item = data.repayPeriodScheduleDtos[i];
            // amount += item.lateRepay-item.payedAmount;
            amount +=item.restRepay
            if(item.repayTime>nowDate) break
        }
        data.restRepay = amount
      };
      if (data.restRepay) {
        amount = +data.restRepay;
        if (couponInfo.useStatus === '2') {
          // 如果优惠券处于使用中，则应还金额减去优惠券金额
          amount = amount - couponInfo.discountAmount;
        } else if (this.couponInfo.checked) {
          // 应产品要求，如果选中了优惠券，则应还金额减去优惠券金额
          amount = amount - couponInfo.discountAmount;
        }
      }
      return amount > 0 ? amount : 0;
    },
  },
  methods: {
    toggleNumQuestion() {
      clearTimeout(this.timer);
      if (!this.isVisibleNumQuestion) {
        this.isVisibleNumQuestion = true;
        this.timer = setTimeout(() => {
          this.isVisibleNumQuestion = false;
        }, 5000);
      } else {
        this.isVisibleNumQuestion = false;
      }
    },
    toggleDetails() {
      this.isVisibleDetails = !this.isVisibleDetails;
    },
    toggleQuestion() {
      clearTimeout(this.timer);
      if (!this.isVisibleQuestion) {
        this.isVisibleQuestion = true;
        this.timer = setTimeout(() => {
          this.isVisibleQuestion = false;
        }, 5000);
      } else {
        this.isVisibleQuestion = false;
      }
    },
    // // 获取优惠券
    // getTickets() {
    //   const params = {
    //     useOrderId: this.orderId,
    //     discountCouponType: 1, // 1还款优惠券 2免息优惠券
    //   };
    //   this.$api.getUserTicketInfo(params).then(res => {
    //     const { status, data } = res;
    //     if ('1' === status && data) {
    //       // 优惠券使用状态 0未使用 1已过期 2使用中 3已使用 4已失效
    //       if (data.useStatus === '2') {
    //         this.couponInfo = data;
    //         this.$emit('couponAmount', data.discountAmount);
    //       } else {
    //         this.$emit('couponAmount', 0);
    //         this.getTicketsList();
    //       }
    //     } else {
    //       this.$emit('couponAmount', 0);
    //       this.getTicketsList();
    //     }
    //   });
    // },
    // // 获取优惠券列表
    // getTicketsList() {
    //   const params = {
    //     useOrderId: this.orderId,
    //     discountCouponType: 1, // 1还款优惠券 2免息优惠券
    //   };
    //   this.$api.getUserTicketInfoListForRepay(params).then(res => {
    //     const { status, data } = res;
    //     if ('1' === status && data) {
    //       this.couponList = data;
    //     }
    //     /* this.couponList = [
    //       { useStatus: '0', serialNumber: 'dfsdf213123', discountAmount: 2000, discountCouponType: '1', dueTime: '2021-09-01' },
    //       { useStatus: '0', serialNumber: 'dfsdf213123', discountAmount: 2000, discountCouponType: '1', dueTime: '2021-09-01' },
    //       { useStatus: '0', serialNumber: 'dfsdf213123', discountAmount: 2000, discountCouponType: '1', dueTime: '2021-09-01' },
    //       { useStatus: '0', serialNumber: 'dfsdf213123', discountAmount: 2000, discountCouponType: '1', dueTime: '2021-09-01' },
    //     ];*/
    //   });
    // },
    // 绑定优惠券
    // useTicket() {
    //   if (this.couponInfo.serialNumber) {
    //     const params = {
    //       useOrderId: this.orderId,
    //       serialNumber: this.couponInfo.serialNumber,
    //     };
    //     this.$api.bindingTicket(params).then(res => {
    //       const { status } = res;
    //       if ('1' === status) {
    //         this.getTickets();
    //         this.showCouponList = false;
    //       }
    //     });
    //   }
    // },
    // 复制还款码
    copy() {
      const me = this;
      // const hybrid = new Hybrid(this.$os.android ? 'android' : 'ios');
      this.$hybrid.action('copyPaste', {
        param: { text: this.repayCode || this.data.contractNumber },
        callback (data) {
          me.$toast('Repayment code copied');
          // console.log(data);
        }
      });
    },
    // 关闭优惠券弹框
    // closeCouponList() {
    //   this.couponInfo = {};
    //   this.showCouponList = false;
    // },
    // 显示优惠券列表
    // handleShowCouponList() {
    //   if (!this.couponInfo.serialNumber) {
    //     this.couponList.sort((a, b) => b.discountAmount - a.discountAmount);
    //     this.couponInfo = this.couponList[0];
    //     this.showCouponList = true;
    //   }
    // },
  },
  created() {
    this.orderId = this.$route.query.orderId;
    // this.getTickets();
  }
};
